from odoo import fields, models


class ChoiceSaleRefundWizard(models.TransientModel):
    _name = 's.choice.sale.refund.wizard'
    _description = 'Choice Sale Refund'

    sb_checking_id = fields.Many2one('sb.account.checking', string='Account Checking')
    line_ids = fields.One2many('s.choice.sale.refund.line.wizard', 'wizard_id')

    def default_get(self, fields_list):
        res = super().default_get(fields_list)
        active_id = self._context.get('active_id')
        checking_id = self.env['sb.account.checking'].browse(active_id)
        account_move_ids = self.env['account.move'].search([
            ('partner_id', '=', checking_id.customer.id),
            ('currency_id', '=', checking_id.currency_id.id),
            ('move_type', '=', 'out_refund'),
            ('sb_checking_id', '=', False)
        ])
        line_vals_list = []
        for move in account_move_ids:
            line_vals_list.append((0, 0, {
                'account_move_id': move.id,
            }))
        res['line_ids'] = line_vals_list
        return res

    def action_confirm(self):
        self.ensure_one()
        self.line_ids.filtered(lambda x: x.choice).mapped('account_move_id').write({'sb_checking_id': self.sb_checking_id.id})


class ChoiceSaleRefundLineWizard(models.TransientModel):
    _name = 's.choice.sale.refund.line.wizard'
    _description = 'Choice Sale Refund Line'

    wizard_id = fields.Many2one('s.choice.sale.refund.wizard')
    account_move_id = fields.Many2one('account.move')
    choice = fields.Boolean('Choice')
